package com.example.lihaoqian

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.fragment.app.Fragment

class AddWordFragment : Fragment() {
    private lateinit var sharedPreferencesUtil: SharedPreferencesUtil

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_add_word, container, false)
        sharedPreferencesUtil = SharedPreferencesUtil(requireContext())

        val editText = view.findViewById<EditText>(R.id.editText)
        val addButton = view.findViewById<Button>(R.id.button_add)

        addButton.setOnClickListener {
            val word = editText.text.toString()
            if (!TextUtils.isEmpty(word)) {
                sharedPreferencesUtil.addWord(word)
                editText.text.clear()
                Toast.makeText(activity, "单词已添加", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(activity, "请输入单词", Toast.LENGTH_SHORT).show()
            }
        }

        return view
    }
}